package org.tgcloud.group.service.manager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.tgcloud.group.core.APPResultMessage;
import org.tgcloud.group.core.ResultMessage;
import org.tgcloud.group.repository.MjBusinessAccountFollowMapper;
import org.tgcloud.group.repository.MjBusinessAccountMapper;
import org.tgcloud.group.repository.MjBusinessPersonMapper;
import org.tgcloud.group.web.controller.dto.businessAccount.AccountCountAreaListDTO;
import org.tgcloud.group.web.controller.dto.businessAccount.AccountCountDTO;
import org.tgcloud.group.web.controller.dto.businessAccount.BusinessAccountProtectDTO;
import org.tgcloud.group.web.controller.dto.businessAccount.BusinessPersonGroupByBusinessTypeDTO;

import javax.xml.transform.Result;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author: zhy
 * @create: 2024-01-09 10:04
 * @description:
 */
@Service
public class BusinessAccountCountManageService {

    @Autowired
    private MjBusinessAccountMapper businessAccountMapper;

    @Autowired
    private MjBusinessAccountFollowMapper businessAccountFollowMapper;

    @Autowired
    private MjBusinessPersonMapper businessPersonMapper;

    public APPResultMessage viewBusinessAccountCount(Date startTime, Date endTime) {
        Map map = new HashMap();
        // 客户总数
        int accountTotalNum = businessAccountMapper.selectAllNum();
        map.put("accountTotalNum", accountTotalNum);
        // 部门新增客户数，部门新增成交客户数（确认签单），部门成交客户总数，部门跟进数
        List<AccountCountDTO> AccountNumGroupByDepart = businessAccountMapper.selectAddNumByTimeGroupByDepart(startTime, endTime);
        // 地区新增客户数，地区新增成交客户数（确认签单），地区成交客户总数，地区跟进数
        List<AccountCountDTO> AccountNumGroupByArea = businessAccountMapper.selectAddNumByTimeGroupByArea(startTime, endTime);
        //
//        AccountNumGroupByDepart = businessAccountMapper.selectAddSignOrderByTimeGroupByDepart(startTime, endTime);
//        List<AccountCountDTO> depart3 = businessAccountMapper.selectAllAddSignOrderNumGroupByDepart();
//        AccountNumGroupByDepart = businessAccountFollowMapper.selectFollowNumByTimeGroupByDepart(startTime, endTime);
        //
//        AccountNumGroupByArea = businessAccountMapper.selectAddSignOrderByTimeGroupByArea(startTime, endTime);
//        List<AccountCountDTO> area3 = businessAccountMapper.selectAllAddSignOrderNumGroupByArea();
//        AccountNumGroupByArea = businessAccountFollowMapper.selectFollowNumByTimeGroupByArea(startTime, endTime);
        map.put("AccountNumGroupByDepart", AccountNumGroupByDepart);
        map.put("AccountNumGroupByArea", AccountNumGroupByArea);

        // 客户区域
        List<AccountCountAreaListDTO> accountCountAreaListDTOS = businessAccountMapper.selectAllNumByTimeGroupByCity(startTime, endTime);
        for (AccountCountAreaListDTO dto : accountCountAreaListDTOS){
            dto.setOtherNum((dto.getTotalNum() != null ? dto.getTotalNum() : 0)
                    - (dto.getAddAccountNum() != null ? dto.getAddAccountNum() : 0));
        }
        map.put("accountCountAreaList", accountCountAreaListDTOS);

        // KA/BD
        List<BusinessPersonGroupByBusinessTypeDTO> businessPersonGroupByBusinessTypeListByKA = businessPersonMapper.selectNumByBusinessType(startTime, endTime, (short) 1);
        map.put("businessPersonGroupByBusinessTypeListByKA", businessPersonGroupByBusinessTypeListByKA);
        List<BusinessPersonGroupByBusinessTypeDTO> businessPersonGroupByBusinessTypeDTOSByBD = businessPersonMapper.selectNumByBusinessType(startTime, endTime, (short) 2);
        map.put("businessPersonGroupByBusinessTypeListByBD", businessPersonGroupByBusinessTypeDTOSByBD);

        // 客保
        BusinessAccountProtectDTO protectDTO = businessAccountMapper.selectAccountProtectNumByTime(startTime, endTime);
        map.put("accountProtectDTO", protectDTO);

        APPResultMessage successMessage = APPResultMessage.getSuccessMessage();
        ResultMessage resultMessage = ResultMessage.builder().setData(map);
        successMessage.setResult(resultMessage);
        return successMessage;
    }
}
